// @ts-check
// import globals from "globals";
import "dotenv/config";
import { createRequire } from "node:module";
import path from "node:path";
import process from "node:process";
// import { FlatCompat } from "@eslint/eslintrc";
import js from "@eslint/js";
import { defineConfig } from "eslint/config";
// import { html, css } from "./global/hl.mjs";
import gitignore from "eslint-config-flat-gitignore";
import webpack from "eslint-config-webpack";
import { configs as dependConfig } from "eslint-plugin-depend";
// import expectType from "eslint-plugin-expect-type/configs/recommended";
import { configs as litConfig } from "eslint-plugin-lit";
import * as nodeDependenciesPlugin from "eslint-plugin-node-dependencies";
import { configs as regexpConfig } from "eslint-plugin-regexp";
import rxjsX from "eslint-plugin-rxjs-x";
import { configs as wcConfig } from "eslint-plugin-wc";
import tseslint from "typescript-eslint";
// import importPlugin from "eslint-plugin-import";

const require = createRequire(import.meta.url);

const angular = require("eslint-plugin-angular");

const isDevelopment = process.env.NODE_ENV !== "production";
/**
 * @power ms-vscode.ts-file-path-support
 * @template TBaseDir
 * @typedef {string & { baseDir?: TBaseDir }} RelativeFilePath
 */

/**
 * @description Resolve relative path to absolute path.
 * @param {RelativeFilePath<'$dir'>} p
 * @returns {string}
 */
const pwd2 = (p) => path.resolve(import.meta.dirname, p);

export default defineConfig(
  {
    name: "ignore",
    ...gitignore(),
  },
  {
    name: "another-ignore",
    ignores: ["./stage3-decorate/**", "./misc-html/**"],
  },
  // {
  //   name: "js",
  //   files: ["./src/**/*.js", "./src/**/*.mjs"],
  //   ...js.configs.recommended,
  // },
  // jsdoc({
  //   config: "flat/recommended",
  //   files: ["./src/**/*.js", "./src/**/*.mjs"],
  // }),
  {
    name: "globals",
    // languageOptions: {
    //   globals: { ...globals.browser, ...globals.node, angular, html, css },
    // },
  },
  // expectType,
  {
    name: "webpack",
    extends: [webpack],
  },
  ...nodeDependenciesPlugin.configs["flat/recommended"],
  {
    name: "depend",
    ...dependConfig["flat/recommended"],
  },

  // {
  // name: "angular",
  // files: ["src/view/**/*.js", "src/view/**/*.mjs"],
  // plugins: {
  //    angular,
  //  },
  //  rules: angular.configs.johnpapa.rules
  // },
  {
    name: "regexp",
    ...regexpConfig["flat/recommended"],
  },
  // {
  //   ...rxjsX.configs.recommended,
  // },
  {
    name: "lit",
    ...litConfig["flat/recommended"],
    files: ["lit-comp/**/*.ts"],
    ignores: ["*.d.ts", "**.js"],
    languageOptions: {
      parser: tseslint.parser,
      parserOptions: {
        projectService: true,
        tsconfigRootDir: pwd2("./lit-comp"),
        emitDecoratorMetadata: true,
        experimentalDecorators: true,
      },
    },
  },
  {
    name: "wc",
    ...wcConfig["flat/recommended"],
    files: ["lit-comp/**/*.ts"],
    ignores: ["*.d.ts", "**.js"],
    languageOptions: {
      parser: tseslint.parser,
      parserOptions: {
        projectService: true,
        tsconfigRootDir: pwd2("./lit-comp"),
        emitDecoratorMetadata: true,
        experimentalDecorators: true,
      },
    },
  },
  {
    name: "override",
    rules: {
      "no-undef": "off",
      "new-cap": "off",
      "id-length": "off",
      "no-unused-expressions": "off",
      "no-use-before-define": "off",
      "no-void": "off",
      "func-names": "off",

      "jsdoc/require-returns-description": "off",
      "jsdoc/require-param-description": "off",
      "jsdoc/check-tag-names": "off",
      "jsdoc/check-alignment": "off",
      "jsdoc/reject-any-type": "off",

      "n/prefer-global/process": "off",
      "n/no-unsupported-features/node-builtins": "off",

      // "@typescript-eslint/no-use-before-define": "off",
      // "@typescript-eslint/no-unused-vars": "error",

      "import/no-named-as-default-member": "off",
      "import/no-unresolved": "off",

      "unicorn/no-abusive-eslint-disable": "off",
      "unicorn/prefer-top-level-await": "off",

      "@stylistic/spaced-comment": "off",
      "spaced-comment": [
        "error",
        "always",
        {
          line: {
            markers: ["/", "!"],
            exceptions: ["+"],
          },
        },
      ],

      "depend/ban-dependencies": "off",

      "@typescript-eslint/consistent-type-definitions": "off",
      "no-duplicate-imports": "off",
      "@typescript-eslint/no-use-before-define": "off",
      "unicorn/prefer-global-this": "off",
      "import/extensions": "off",
      // "import/consistent-type-specifier-style": ["error", "prefer-top-level"],
      // "n/no-unsupported-features/es-syntax": [
      //   "error",
      //   {
      //     version: ">=22.12.0",
      //     ignores: [],
      //   },
      // ],
    },
    settings: {
      "import/resolver": {
        webpack: {
          config: pwd2("./rspack.config.cjs"),
        },
      },
      "import/ignore": [
        "\\.(less|css)$", // can't parse unprocessed CSS modules, either
        "\\.(html|tpl)$", // can't parse unprocessed HTML modules, either
      ],
      // 'import/resolver': {
      //   alias: {
      //     "@/*": "./src/*",
      //   },
      // }
    },
  },
  {
    name: "dev-override",
    rules: {
      "no-console": isDevelopment ? "off" : "error",
      "no-debugger": isDevelopment ? "off" : "error",
      "no-unused-vars": isDevelopment ? "off" : "error",
      "@typescript-eslint/no-unused-vars": isDevelopment ? "off" : "error",
    },
  },
);
